package cn.itcast.hotel;

import cn.itcast.hotel.constants.HotelConstants;
import org.apache.http.Header;
import org.apache.http.HttpHost;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.common.xcontent.XContentType;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author mrchen
 * @date 2022/2/16 14:32
 */
@SpringBootTest
public class HotelIndexDemo {
    RestHighLevelClient client;

    @Test
    public void createIndexAtCluster() throws IOException {
        // 1.  创建对应的请求对象  Request
        CreateIndexRequest request = new CreateIndexRequest("hotel");
        //       设置不同的请求参数
        Map map = new HashMap<>();
        map.put("number_of_shards",3);
        map.put("number_of_replicas",3);
        request.settings(map);
        // 2.  调用客户端对象执行对应的方法
        CreateIndexResponse response = client.indices().create(request, RequestOptions.DEFAULT);
        // 3.  解析相应对象  Response
        System.out.println(response.isAcknowledged()?"索引库创建成功":"创建失败");
    }


    @Test
    public void existIndex() throws IOException {
        // 1.  创建对应的请求对象  Request
        GetIndexRequest request = new GetIndexRequest("hotel");
        // 2.  调用客户端对象执行对应的方法
        boolean exists = client.indices().exists(request, RequestOptions.DEFAULT);
        // 3.  解析相应对象  Response
        System.out.println(exists?"存在":"不存在");
    }
    @Test
    public void deleteIndex() throws IOException {
        // 1.  创建对应的请求对象  Request
        DeleteIndexRequest request = new DeleteIndexRequest("hotel");
        // 2.  调用客户端对象执行对应的方法
        AcknowledgedResponse response = client.indices().delete(request, RequestOptions.DEFAULT);
        // 3.  解析相应对象  Response
        System.out.println(response.isAcknowledged()?"删除成功":"删除失败");
    }

    @Test
    public void createIndex() throws IOException {
        // 1.  创建对应的请求对象  Request
        CreateIndexRequest request = new CreateIndexRequest("hotel");
          //       设置不同的请求参数
        request.source(HotelConstants.MAPPING_TEMPLATE, XContentType.JSON);
        // 2.  调用客户端对象执行对应的方法
        CreateIndexResponse response = client.indices().create(request, RequestOptions.DEFAULT);
        // 3.  解析相应对象  Response
        System.out.println(response.isAcknowledged()?"索引库创建成功":"创建失败");
    }
    @BeforeEach
    public void init(){
        client = new RestHighLevelClient(
                RestClient.builder(
                        HttpHost.create("http://192.168.200.130:9200"),
                        HttpHost.create("http://192.168.200.130:9201"),
                        HttpHost.create("http://192.168.200.130:9202")
                ));
    }
    @AfterEach
    public void close(){
        try {
            client.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
